﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class ThanhvienDAL: DAL.BaseDAL
    {
        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Thanhvien");
        }

        /// <summary>
        /// Select User
        /// </summary>
        /// <param name="_user"> _user</param>
        /// <returns> data table </returns>
        public DataTable Select(string _user)
        {
            try
            {
                string sql = @"Select * From tb_Thanhvien Where Ten = '{0}' collate Latin1_General_CS_AS ";
                sql = string.Format(sql, _user);
                return ExecuteQuery(sql);
            }
            catch { return null; }
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            var sql = @"select taikhoan,matkhau,vaitro,ten_nv from Nhanvien where Taikhoan = '{0}' collate Latin1_General_CS_AS";
            sql = String.Format(sql, key);
            var tbl = ExecuteQuery(sql);
            try
            {
                if (tbl.Rows.Count > 0)
                {
                    var usr = new Entities.Thanhvien();
                    usr.taikhoan = tbl.Rows[0]["Taikhoan"] + "";
                    usr.matkhau = tbl.Rows[0]["Matkhau"] + "";
                    usr.hoten = tbl.Rows[0]["ten_nv"] + "";
                    usr.vaitro = Convert.ToInt32(tbl.Rows[0]["Vaitro"]);
                    return usr;
                }
                else return null;
            }
            catch { return null; }
        }

        //public bool Delete(string id)
        //{
        //    throw new NotImplementedException();
        //}

        //public bool Delete(long id)
        //{
        //    var sql = "sp_Delete_Thanhvien {0}";
        //    sql = string.Format(sql, id);
        //    return ExecuteNonQuery(sql) > 0 ? true : false;
        //}

        //public bool Insert(object obj)
        //{
        //    var o = (Thanhvien)obj;
        //    var sql = "set dateformat dmy; execute sp_Insert_Thanhvien '{0}', '{1}', N'{2}', '{3}' ";
        //    sql = string.Format(sql, o.Ten, o.Matkhau, o.Hoten, o.VaitroID);
        //    return ExecuteNonQuery(sql) > 0 ? true : false;
        //}

        //public bool Update(object obj)
        //{
        //    var o = (Thanhvien)obj;
        //    var sql = "set dateformat dmy; execute sp_Update_Thanhvien '{0}', '{1}', '{2}', N'{3}', '{4}' ";
        //    sql = string.Format(sql, o.Id, o.Ten, o.Matkhau, o.Hoten, o.VaitroID);
        //    return ExecuteNonQuery(sql) > 0 ? true : false;
        //}

        //public bool Update(string _user, string pass)
        //{
        //    DataTable dtb = Select(_user);
        //    long _id = Convert.ToInt64(dtb.Rows[0]["Id"]);

        //    string sql = @"Update tb_Thanhvien Set Matkhau = '{0}' Where Id = '{1}'   ";
        //    sql = string.Format(sql, pass, _id);
        //    return ExecuteNonQuery(sql) > 0 ? true : false;
        //}

        //public DataTable Search(string name)
        //{
        //    var sql = @"select * from tb_Thanhvien where Ten like N'%{0}%' ";
        //    sql = string.Format(sql, name);
        //    return ExecuteQuery(sql);
        //}

        
    }
}
